/*
 * Created by Asnull.
 * Website:https://lipux.cn/
 * Date:2023-4-1
 */
const express = require('express');
const cors = require('cors');
const userAgent = require('express-useragent');
const api = require('./api_handle');
const config = require('./config');

// 创建服务器实例
const app = express();
// 监听端口
app.listen(3031, '0.0.0.0', () => {
    console.log(`服务器启动成功！
- PC端：\x1B[36mhttp://localhost:3031/pc/\x1B[0m
- 移动端：\x1B[36mhttp://localhost:3031/m/\x1B[0m\n
- Created by \x1B[31mAsnull\x1B[0m.
- Know more：https://lipux.cn/`);
})

// 挂载静态资源
app.use('/hw', express.static('./hw'))
app.use('/pc', express.static('./pc'))
app.use('/m', express.static('./m'))

// 设备判断中间件
app.use(userAgent.express());
// 配置解析表单数据的中间件
app.use(express.urlencoded({ extended: false }))
app.use(express.json());

// 解决跨域
app.use(cors());

// 自动设别设备类型重定向
app.get('/', function(req, res) {
    if (req.useragent.isMobile) {
        res.redirect(301, config.webUrl + '/m');
    } else {
        res.redirect(301, config.webUrl + '/pc');
    }
});

//挂载路由
app.post('/login', api.login);
app.post('/getChatList', api.getChatList)
app.post('/getArticle', api.getArticle)
app.post('/getTemplateList', api.getTemplateList)
app.post('/makeArticle', api.makeArticle)
app.post('/getChat', api.getChat)
app.post('/ai', api.ai)
app.get('/chat', api.chat)